package com.sh.data.engine.util;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;

/**
 * @Auther: zigui.zdf @Date: 2018/10/15 17:12 @Description:
 */
@Slf4j
public class DateUtil {

    /**
     * 格式化时间
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return date == null ? null : dateFormat.format(date);
    }

    public static String formatDate(long time, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return time == 0 ? null : dateFormat.format(new Date(time));
    }

    /**
     * 默认的格式
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        String format = "yyyy-MM-dd HH:mm:ss";
        return formatDate(date, format);
    }

    /**
     * 时间字符串转long类型
     *
     * @param date
     * @param format
     * @return
     */
    public static Long toLong(String date, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        try {
            Date parse = dateFormat.parse(date);
            return parse.getTime();
        } catch (ParseException e) {
            log.error("时间转换失败", e);
        }
        return null;
    }

    public static Long toLong(String date) {
        return toLong(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 返回当前日期的 想个的某一时刻的时间，amount为正数 当前时间后的时间， 为负数 当前时间前的时间
     *
     * @param d      ,某一个日期
     * @param field  日历字段 y 年 M 月 d 日 H 时 m 分 s 秒
     * @param amount 数量
     * @return 一个日期
     */
    public static Date getPerDate(Date d, String field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        if (field != null && !"".equals(field)) {
            if ("y".equals(field)) {
                calendar.add(Calendar.YEAR, amount);
            } else if ("M".equals(field)) {
                calendar.add(Calendar.MONTH, amount);
            } else if ("d".equals(field)) {
                calendar.add(Calendar.DAY_OF_MONTH, amount);
            } else if ("H".equals(field)) {
                calendar.add(Calendar.HOUR, amount);
            } else if ("m".equals(field)) {
                calendar.add(Calendar.MINUTE, amount);
            } else if ("s".equals(field)) {
                calendar.add(Calendar.SECOND, amount);
            }
        } else {
            return null;
        }
        return calendar.getTime();
    }

    /**
     * 今天开始时间
     *
     * @return
     */
    public static Date getTodayStart(Date date) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 今天结束时间
     *
     * @return
     */
    public static Date getTodayEnd(Date date) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return todayEnd.getTime();
    }


    public static long judgeConvertToTimestamp(String input) {

        try {
            DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("yyyy-MM-dd'T'HH:mm:ss")
                .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
                .appendOffset("+HH:mm", "Z")
                .toFormatter();
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(input, formatter);
            LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
            ZoneOffset zoneOffset = offsetDateTime.getOffset();
            Instant instant = localDateTime.toInstant(zoneOffset);
            long timestamp = instant.toEpochMilli();
            return timestamp;
        } catch (Exception e) {
            return -1; // Return -1 if the input does not match any format
        }
    }
}
